<?php

class m_Base extends CI_Model {

    function __construct()
    {	
        parent::__construct();		
    }	
        
    function init(){
            $this->_table = $this->db->dbprefix . $this->_table;
    }
    
    function insert($entity)
    {
            return $this->db->insert($this->_table, $entity);
    }

    function update($entity,$where=NULL)
    {
            if($where)
                $where = array($this->_pk => $entity[$this->_pk]);
			unset($entity[$this->_pk]);
            return $this->db->update($this->_table, $entity, $where);
    }

    function delete($where)
    {
            if(!is_array($where))
                    $where = array($this->_pk => $where);

            return $this->db->delete($this->_table, $where);
    }

    function select_one($where){
            if(!is_array($where))
                    $where = array($this->_pk => $where);
            $this->db->select('*')->from($this->_table)->where($where)->limit(1);
            $query = $this->db->get();
            $entity = $query->result();
            if($entity)
            return $entity[0];
            else
                    return null;
    }

    function select_list($where = null, $orderby = null,$limit = null, $offset = null){
            $this->db->select('*')->from($this->_table);
            if($where) $this->db->where($where);
            if($orderby) $this->db->order_by($orderby);
            if($limit) $this->db->limit($limit,$offset);		
            $query = $this->db->get();
            $entity = $query->result();
            if($entity)
            return $entity;
            else
                    return null;
    }

    function get_pager($page_size, $where = null){
            $this->db->select('count(0) as total')->from($this->_table);
            if($where) $this->db->where($where);	
            $query = $this->db->get();
            $entity = $query->result();
            $res = array(
                    'size' => $page_size,
                    'total' => 0,
                    'page_count' => 0);			
            if($entity){
                    $res['total'] = $entity[0]->total;
                    $res['page_count'] = ceil($res['total'] / $page_size);
            }
            return $res;
    }
}
?>